/***************************************************************************
 *   Copyright (C) 2008 by Justin L. Boss                                  *
 *   justin@rtechguys.com                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef SEACREATURE_H
#define SEACREATURE_H

#include <fstream>
#include <math.h>
#include <QTime>
#include "guidancesystem.h"

struct MeshData
{
    GLfloat vx, vy, vz, nx, ny, nz;
};

struct SMeshData
{
    GLfloat vx, vy, vz, nx, ny, nz, bv;
    GLint bi;
};

struct FaceData
{
    GLuint a, b, c, d;
};

struct UVData
{
    GLfloat x,y;
};

using namespace std;

static const GLfloat PI_180 = 0.01745329251994329577; //3.14159265358979323846/180

static const GLfloat TailPath[56][2]={{-6.0,0},{-5.5,-2},{-5.0,-4},{-4.5,-6},{-4.0,-7},{-3.5,-8},{-3.0,-9},{-2.5,-10},{-2.0,-11},{-1.5,-12},{-1.0,-12},{-0.5,-12},{0.0,-12},{0.5,-12},{1.0,-12},{1.5,-12},{2.0,-12},{2.5,-12},{3.0,-12},{3.5,-12},{4.0,-11},{4.5,-10},{5.0,-9},{5.5,-8},{6.0,-7},{6.5,-6},{7.0,-4},{6.5,-2},{6.0,0},{5.5,2},{5.0,4},{4.5,6},{4.0,7},{3.5,8},{3.0,9},{2.5,10},{2.0,11},{1.5,12},{1.0,12},{0.5,12},{0.0,12},{-0.5,12},{-1.0,12},{-1.5,12},{-2.0,12},{-2.5,12},{-3.0,12},{-3.5,12},{-4.0,11},{-4.5,10},{-5.0,9},{-5.5,8},{-6.0,7},{-6.5,6},{-7.0,4},{-6.5,2}};

static const GLfloat Identity [] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};

class SeaCreature : public GuidanceSystem
{
private:
    QTime Time;
    int LastTime;
protected:
    GLfloat TailSpan;
    GLfloat RX;
    GLfloat RY;
    GLfloat RZ;
    GLfloat *CurMtx;						// The Matrix for X Rotation
    GLfloat *OldMtx;						// The Old matrix after the new one is created
    GLfloat *MtxRtX;						// Matrix for X Rotation
    GLfloat *MtxRtY;						// Matrix for Y Rotation
    GLfloat *MtxRtZ;						// Matrix for Z Rotation
    GLfloat *MtxTrn;						// Matrix for Translation
    GLfloat **BonMtx;						// Matrix for each bone
    GLfloat TailPathCount;
    int BoneSize;
    int MeshSize;
    int FaceSize;
    int UVCordSize;
    int TextureName;
    int HalfBone;
    GLfloat *Bones;
    FaceData *Faces;
    UVData *UVCord;
    MeshData *TransMesh;
    SMeshData *StaticMesh;
    void CatMtx (GLfloat *);
    void LoadIdentity();
    void Rotate (GLfloat, GLfloat, GLfloat);
    void Translate (GLfloat, GLfloat, GLfloat);
    void Scale (GLfloat, GLfloat, GLfloat);
    void UpdateTail();
    void CpyMtx(GLfloat *destmatrix);
public:
    bool Mammal;
    GLfloat Size;
    void SetSchoolPatern(GLfloat patern[][2]);
    SeaCreature (const char[]);
    ~SeaCreature();
    void SetTexture (GLuint name);
   void Draw (GLfloat offset, int speed, GLfloat hight);
    void Draw (GLfloat offset, int speed, GLfloat gsMatrix[]);
    void scRotate(GLfloat X, GLfloat Y, GLfloat Z);
    void SetTailSpan(int span);
    void SetSize(int size);
};

#endif
